<?php
defined('BASEPATH') or die('forbidden');
class Dailycopy_Controller Extends Controller
{
    private $m;    //数据库连接
    private $server_info;
    private $copoyid2name;
    private $dailycopyid2difficulty;
    private $restype2name;
    
    function _init()
    {
        check_level(50,segment(0),segment(1));
        set_time_limit(0);
        $this->inc = $this->config['inc'];
        
        //得到选择的区号
        $areanum = $_SESSION['area_selected_id'];
        if ($areanum == '') 
        {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
            return;
        }

        $selectSql = "SELECT * FROM server_mapping WHERE dbtype='1' and areanum='$areanum' ";
        $this->db->query($selectSql);
        $this->server_info=$this->db->get_array('default');

        // db数据库
        $this->m = new MongoClient("mongodb://{$this->server_info['dbip']}");
        if (false == $this->m) {
            //内部有提示错误，在此就不提示了
            //echo "<script>alert(' connect areanum:$areanum error ');</script>";
            return;
        }

        $this->copoyid2name = array();
        $copysql =  "SELECT * FROM tt_copyid2name WHERE type='4' ";
        $this->db->query ( $copysql );
        while ( $n = $this->db->get_array () ) {
            $this->copoyid2name[(int)$n ['id']] = $n ['name'];
        }

        $this->dailycopyid2difficulty = array();
        $copysql2 =  "SELECT * FROM tt_dailyresid2copyid  ";
        $this->db->query ( $copysql2 );
        while ( $n = $this->db->get_array () ) {
            $resdailycopy = array('resid' => (int)$n['resid'], 'difficulty' => (int)$n['difficulty']);
            $this->dailycopyid2difficulty[(int)$n ['copyid']] = $resdailycopy;
        }

        $this->restype2name = array(1=>'巧克力蛋挞',
            2=>'金币',
            3=>'进阶琥珀',
            4=>'稀有经验圣物',
            5=>'超级精炼结晶',
            6=>'圣物精炼卷轴',
        );
    }

    function index()
    {
        $this->view->display("playercopy/dailycopy/index");
    }

    function flexigrid()
    {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $query=$_POST['query'];
        $query_type=$_POST['qtype'];
        if($query_type=='_id'){
            $filter=array($query_type=>(float)$query);
        }else{
            $filter=array($query_type=>$query);
        }
        $collection=$this->m->{$this->server_info['dbname']}->PlayerCopy;
        if($query!=''){
            $document=$collection->find($filter);
        }else{
            $document=$collection->find();
        }

        date_default_timezone_set("PRC");
        $data=array();
        $total = 0;
        foreach ($document as $key=>$value){
            foreach ($value['daily']['copylst'] as $k=>$v){
                $d['_id']=$value['_id'];
                $d['name'] = $this->restype2name[(int)$v['resid']];
                
                if ((bool)$v['ischallenge'] == true){
                    $d['challengetimes'] = 1;
                    $d['lastchallengetime']=get_datetime($v['lastchallengetime']);
                    $d['difficulty']=$this->dailycopyid2difficulty[(int)$v['copyid']]['difficulty'];
                }else{
                    $d['challengetimes'] = 0;
                    $d['lastchallengetime']=0;
                    $d['difficulty']=0;
                }
                
                $num=$key.$k;
                $data[$num]=array('id' => $value['_id'], 'cell' => $d);
                $total += 1;
            }
        }
        $result = array(
                'page' => $page,
                'total' => $total,
                'rows' => $data
        );

        //$this->dbConnt->free_all();
        echo json_encode($result);
    }

    function del($id){

    }
}
?>